package com.listeningframework.boot.autoconfigure.webmvc.validate;

import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.Maps;

/**
 * 
 * Validation Result
 *
 * @author Listening
 * @version $Id: ValidationResult.java, v 0.1 2016年5月5日 下午9:12:47 Listening Exp $
 */
public class ValidationResult {
    /**
     * 校验结果是否有错
     */
    private boolean             hasErrors   = false;
    /**
     * 校验错误信息
     */
    private Map<String, String> errorMsgMap = Maps.newHashMap();

    public boolean hasErrors() {
        return hasErrors;
    }

    public void setHasErrors(boolean hasErrors) {
        this.hasErrors = hasErrors;
    }

    public void addErrorMsg(String key, String value) {
        if (errorMsgMap == null) {
            this.errorMsgMap = Maps.newHashMap();
        }
        this.errorMsgMap.put(key, value);
    }

    public String getErrorMsg() {
        if (errorMsgMap == null || errorMsgMap.isEmpty()) {
            return "";
        }
        int index = 1;
        StringBuffer buffer = new StringBuffer();
        for (Entry<String, String> entry : errorMsgMap.entrySet()) {
            index += 1;
            buffer.append(index).append(". ").append(entry.getValue()).append("  ");
        }
        return buffer.toString();
    }

    @Override
    public String toString() {
        return "ValidationResult [hasErrors=" + hasErrors + ", errorMsg=(" + getErrorMsg() + ")]";
    }
}